﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Event_SG_EWDT
{
    public class MemberControls // This Control stores all the business logic of Members & Account
    {
        // When user wants to register to be a member, user just have to call Registration();
        // When user wants to change password for his account, he just have to call UpdatePassword();
        // When user wants to login, the system calls VerifyAccount()


        public static bool Registration(string account, string password, string Name, string Handphone, string email,int interest)
        {
            if (password == "" || Name == ""|| email == "")
            {
                return false;
            }

            Member tempMember = new Member();
            tempMember.Name = Name;
            tempMember.Handphone = Handphone;
            tempMember.Email = email;
            tempMember.interest = interest;
            tempMember.MembershipNo = RegisterMember(Name, Handphone, email, interest);
            if (tempMember.MembershipNo != null)
            {
                if (RegisterAccount(tempMember.MembershipNo, account, password))
                {
                    return true;
                }
                else {
                    DBMANAGER.DeleteMember(tempMember);
                    return false; }
                
            }
            else return false;
        }


        #region Account
        public static bool RegisterAccount(string membership_No,string account, string password)
        {
            try
            {
                Account tempA = new Account();
                tempA.Account_Name = account;
               // tempA.Password = password;
                tempA.Password = Utility.GenerateHashFromPassword(password);
                tempA.Membership_No = membership_No;

                return DBMANAGER.CreateAccount(tempA);

            }
            catch
            {
                return false;
            }
        }
        public static Member VerifyAccount(string account, string password)
        {

            if (DBMANAGER.RetrieveAccount(account, Utility.GenerateHashFromPassword(password)))  
            {
                return RetrieveMemberByAccount(account);
                
            }

           return null; 
        } 
        public static bool UpdatePassword(string memberid, string password)
        {
            String account = MemberControls.RetrieveAccountByMember(memberid);
            // Update to DB here.
            Account a = new Account();
            a.Account_Name = account;
            a.Membership_No = memberid;
            a.Password = password;
            if (DBMANAGER.UpdatePassword(a))
            {
                return true;
            }
            else
            {
                return false;
            }
                
        }
        public static string RetrieveMemberPassword(string memberid)
        {
            string pass = DBMANAGER.RetrievePassword(memberid);
             return pass;
        }
        public static bool IsAccountExist(string account_email)
        {
            Member tempM = MemberControls.RetrieveMemberByAccount(account_email);
            if (tempM != null)
            {
                return true;
            }
            return false;
        }

        #endregion

        // Registering call RegisterMember()
        // Updating Info call UpdateMember()


        #region Member
        public static string RegisterMember(string Name, string Handphone, string email, int interest)// Done
        {
            try
            {
                Member tempMember = new Member();
                tempMember.Name = Name;
                tempMember.Handphone = Handphone;
                tempMember.Email = email;
                tempMember.interest = interest;

                tempMember.MembershipNo = DBMANAGER.CreateMember(tempMember);
                Member.listMember.Add(tempMember); // add into the List as well.
                

                return tempMember.MembershipNo;
            }
            catch
            {
                return null;
            }
        } 
        public static Member RetrieveMember(string membershipNO)
        {
           Member TempM= DBMANAGER.RetrieveMember(membershipNO);
           return TempM;
        } //Done
        public static Member RetrieveMemberByAccount(string account)
        {
            string s = DBMANAGER.RetrieveMemberByAccount(account);
            if (s != "")
            {
                return DBMANAGER.RetrieveMember(s);
            }
            // get the member info then return.
            return null;

            //return a null if it is not found.
        }
        public static bool UpdateMember(string membershipNO, string Name, string Handphone, int interest)
        {
            try
            {
                Member m = new Member();
                m.MembershipNo = membershipNO;
                m.Name = Name;
                m.Handphone = Handphone;
                m.interest = interest;
                DBMANAGER.UpdateMember(m);

                return true;
            }
            catch
            {
                return false;
            }
        } //Done
        public static string RetrieveAccountByMember(string memberID)
        {
            return DBMANAGER.RetrieveAccount(memberID);

        }
        #endregion



  
    }
}